<template>
    <!--pass 0 交易密码  1为登录密码-->
    <div class="setpass-view youke">
        <van-nav-bar
                :title="$route.query.pass==0?'交易密码':'登录密码'"
                left-arrow
                fixed
                @click-left="onBack()"
                @click-right="sendFun"
        >
            <van-button
                    slot="right"
                    size="small"
                    type="info"
            >保存
            </van-button>
        </van-nav-bar>
        <Main>
            <template slot="main">
                <div class="inputs-group">
                    <van-cell-group>
                        <van-field
                                v-model="requestParam.passwo1"
                                required
                                type="password"
                                clearable
                                label="旧密码"
                                :placeholder="$route.query.pass==0?'请输入旧的交易密码':'请输入旧的登录密码'"
                        />

                        <van-field
                                v-model="requestParam.passwo2"
                                type="password"
                                label="新密码"
                                :placeholder="$route.query.pass==0?'请输入新交易密码':'请输入新登录密码'"
                                required
                        />
                        <van-field
                                v-model="checkPass"
                                type="password"
                                label="确认密码"
                                :placeholder="$route.query.pass==0?'请再次输入新交易密码':'请再次输入新登录密码'"
                                required
                        />
                    </van-cell-group>
                    <div class="tips">*温馨提示：请记住新设置的密码，不要告知他人，以保证账户的安全。</div>
                    <div
                            class="btn-view"
                            @click="forgetFun"
                    >忘记密码
                    </div>
                </div>
            </template>
        </Main>
    </div>
</template>

<script type="text/ecmascript-6">
    import {user} from "@/api";

    export default {
        //接收父级传递的参数
        props: [],
        //监听数据变化
        watch: {},
        data() {
            return {
                requestParam: {
                    passwo1: "",
                    passwo2: ""
                },

                checkPass: ""
            };
        },
        //实例初始化之前触发事件
        beforeCreate() {
        },
        //实例化之后触发事件
        created() {
        },
        //实时监控data参数数据变化
        computed: {},
        //模板组件
        components: {},
        //执行方法
        methods: {
            // 提交
            sendFun() {
                // 0为交易密码，1为登录密码
                if (!this.requestParam.passwo1) {
                    this.$toast.fail(
                        `${this.$route.query.pass == 0 ? "请输入旧交易密码" : "请输入旧登录密码"}`
                    );
                    return;
                }
                if (!this.requestParam.passwo2) {
                    this.$toast.fail(
                        `${this.$route.query.pass == 0 ? "请输入新交易密码" : "请输入新登录密码"}`
                    );
                    return;
                }
                if (!this.checkPass) {
                    this.$toast.fail(
                        `${
                            this.$route.query.pass == 0 ? "请输入确认交易密码" : "请输入确认登录密码"
                            }`
                    );
                    return;
                }
                if (this.checkPass != this.requestParam.passwo2) {
                    this.$toast.fail(`两次输入密码不一致`);
                    return;
                }
                let _data = {};
                if (this.$route.query.pass == 0) {
                    _data.passwo1 = this.requestParam.passwo1;
                    _data.passwo2 = this.requestParam.passwo2;
                    user.setdealPwd(_data).then(res => {
                        this.$toast(res.reason);
                    });
                } else {
                    _data.code1 = this.requestParam.passwo1;
                    _data.code2 = this.requestParam.passwo2;
                    let that = this;
                    user.setloginPwd(_data).then(res => {
                        if (res.code == 1000) {
                            this.$toast(res.reason);

                            setTimeout(function(){
                                that.$router.push("/user/login");

                            },1000);
                            // 清除存储
                            this.$store.dispatch("clearlocal");
                        }
                    });
                }
            },
            // 忘记密码
            forgetFun() {
                this.$router.push({
                    path: "/user/forget",
                    query: {pass: this.$route.query.pass}
                });
            }
        },
        mounted() {
        }
        //路由退出时候调用
        //beforeRouteLeave(to, from, next){next()}
    };
</script>

<style scoped lang="less">
    .inputs-group {
        .tips {
            padding: 1rem 2rem;
            color: #666666;
        }

        .btn-view {
            width: 90%;
            height: 3.125rem;
            line-height: 3.125rem;
            text-align: center;
            color: #999999;
            background: #fff;
            margin: 0 auto;
            border-radius: 4px;
        }
    }
</style>
